package com.test.auth_db_auto.controller;

import com.test.auth_db_auto.model.User;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.endpoint.FrameworkEndpoint;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @ Author: .Mr
 * @ ClassName RevokeTokenEndpoint
 * @ Description TODO
 * @ date 2022/1/14 15:53
 * @ Version 1.0
 */
@FrameworkEndpoint
public class RevokeTokenEndpoint {

    @Autowired
    @Qualifier("consumerTokenServices")
    ConsumerTokenServices consumerTokenServices;

    /**
     * 账号注销登录  （在浏览器测试）
     * */
    @RequestMapping(method = RequestMethod.GET, value = "/token/revoke")
    @ResponseBody
    public Object revokeToken(String access_token){
        Map<String,String> info=new HashMap<>();

        if (consumerTokenServices.revokeToken(access_token)){
            //清除用户信息，防止再次请求http://localhost:8102/oauth/authorize?client_id=cms&response_type=code时跳过登录，直接返回授权码
            SecurityContextHolder.getContext().setAuthentication(null);
            info.put("info","成功");
        }else {
            System.out.println("----------------------->>>>>>>>>>>>>>>>>>>>>>>失败");
            info.put("info","失败");
        }
        return info;
    }
}
